#include "./view.h"

/*********SGUI 文本模板************
使用方法：
//在自定义draw调用sGuiTextDraw
sGuiTextDraw(sObj*,sCam*);

占用资源：为了和button混用
obj->data[8] _FNT指针
obj->data[9] 文本
*********************************/

//绘制文字
void sGuiDrawText(void*ob)
{
 sObj*obj = ob;
 sCam*cam = &(_room->cam);
 sGuiDrawTextPos(obj,cam,0,0);
}

//创建文本
sObj* sGuiNewText(int x,int y,char*text,_FNT*fnt)
{
 sObj*obj = sNewObj(x,y,NULL);
 obj->draw = sGuiDrawText;
 obj->sprw = fnt_getLineW(fnt,text);
 obj->sprh = fnt_getLineW(fnt,"A");
 obj->data[8] = (int)fnt;
 obj->data[9] = 
 getRes(text,tStr)->id;
 return obj;
}

//设置控件文本
int sGuiSetText(sObj*obj,char*text)
{
 if(obj==NULL)
  return -1;
 obj->data[9] = 
 getRes(text,tStr)->id;
 return 0;
}

//绘制文本
void sGuiDrawTextPos(sObj*obj,sCam*cam,int dx,int dy)
{
 _FNT* fnt = (_FNT*)obj->data[8];
 char *data = (char*)obj->data[9];
 int sprx = 0;
 int spry = 0;
 if(obj->spr!=NULL)
 {
  sprx = obj->spr->x;
  spry = obj->spr->y;
 }
 //精灵坐标
 int x = obj->x-sprx-cam->x;
 int y = obj->y-spry-cam->y;
 //计算居中
 int pw = fnt_getLineW(fnt,data);
 int px = (obj->sprw-pw)/2;
 int ph = fnt_getLineW(fnt,"W");
 int py = (obj->sprh-ph)/2;
 
// clipRect(x,y,obj->sprw,obj->sprh);
 fnt_drawline(fnt,data,
 x+dx+px,y+dy+py);
 clipRect(0,0,SCRW,SCRH);
}



